/**
 * Javascripts for Cosa
 * 
 */



/**
 * Page loader jQuery
 * 
 */

	$(document).ready(function() {
		$.toggleNext.init();
		hideMessagesDelayed();
	});



/**
 * Validate login
 * 
 */

	function validateLogin(f) {
		if(f.validate.value == 'true') {
			if(f.uname.value == '' || f.pass.value == '') {
				$('#login-failed').html(lang['login_error_fields']);
				$("#login-failed").show('slow');
			} else {
				$.post("kernel/lib/ajax/validateLogin.php", { uname: f.uname.value } , function(r) {
					if (r == 'true') {
						f.validate.value = 'false';
						f.submit();
					} else {
						$("#login-failed").html(r);
						$("#login-failed").show('slow');
					}
				});
			}
			return false;
		} else return true;
	}



/**
 * Update language dropdown after changing domain dropdown
 * 
 */
	
	function updateLanguageDropdown(dropdown_id, domain_id) {
		$.post("kernel/lib/ajax/getLanguagesFromDomain.php", { id: domain_id } , function(r) {
			$('#'+dropdown_id).html(r);
		});
	}



/**
 * Toggle divs from select
 * 
 */

	function toggleDivsFromSelect(s) {
		var numOpt = s.length;
		for(var i=0; i<numOpt; i++) {
			if(s.value == s[i].value) $('#'+s[i].value).show();
			else $('#'+s[i].value).hide();
		}
	}



/**
 * Fadeout messages on page load
 * 
 */
	
	var to;
	function hideMessagesDelayed() {
		clearTimeout(to);
		to = setTimeout(function() { $('.messages').fadeOut('slow', function() { clearMessages(); }); }, 3000);
	}



/**
 * Show message and fade in
 * 
 */
	
	function showMessage(t) {
		$('.messages').html('<li>'+t+'</li>').fadeIn();
		hideMessagesDelayed();
	}



/**
 * Clear messages in cache
 * 
 */
	
	function clearMessages() {
		$('.messages').html('');
	}



/**
 * Add message to the pool
 * 
 */
	
	function addMessage(t) {
		var c = $('.messages').html();
		$('.messages').html(c+'<li>'+t+'</li>');
	}



/**
 * Show messages
 * 
 */
	
	function showMessages() {
		$('.messages').fadeIn();
		hideMessagesDelayed();
	}



/**
 * Show loader
 * 
 */
	
	function showLoader(d) {
		if($(d)) $(d).html('<img src="'+cfg['base_url']+'/kernel/files/img/layout/loader.gif" alt="" />').fadeIn();
	}



/**
 * ToggleNext for cosa
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 */
 
	$.extend({
		toggleNext: {
			init: function() {
				this.speed = 400;				// animation speed after the click
				this.trigger = '.toggleNext';	// id or class name for the trigger
				this.openClass = 'open';		// class for the handler if the next item is open
				this.closeNext();				// closes all the next items if the openClass is not set in the handler
				this.addActions();				// add the click actions to the handlers
			},
			closeNext: function() {
				$(this.trigger).each(function() {
					var handler = $(this);
					var next = handler.next();
					if(handler && next && !handler.hasClass($.toggleNext.openClass)) next.hide();
				});
			},
			addActions: function() {
				$(this.trigger).click(function(evt) {
					var handler = $(this);
					var next = handler.next();
					if(handler && next) {
						var openit = (next.css('display') == 'block') ? 0 : 1;
						if(openit) {
							next.slideDown($.toggleNext.speed);
							handler.addClass($.toggleNext.openClass);
						} else {
							next.slideUp($.toggleNext.speed);
							handler.removeClass($.toggleNext.openClass);
						}
						evt.preventDefault();
					}
				});
			}
		}
	});


